package com.example.controller.institutioncare.congyerenyuan;

import com.example.bean.institutioncare.congyerenyuan.CongYeVo;
import com.example.bean.login.TUserInfo;
import com.example.config.MD5;
import com.example.service.institutioncare.congyerenyuan.CongYeRenYuanService;
import com.example.utils.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/congye")
public class CongYeRenYuanController {

    @Autowired
    CongYeRenYuanService congYeRenYuanService;

    @PostMapping("/page")
    public PageInfo<TUserInfo> congyefenye(@RequestBody CongYeVo congYeVo){
        return congYeRenYuanService.congyepage(congYeVo);
    }

    @PostMapping("/addcongye")
    public Result tianjiacongye(@RequestBody TUserInfo tUserInfo){
        String newPassword = MD5.inputPassToFormPass(tUserInfo.getUserPassword());
        System.out.println("新密码："+newPassword);
        tUserInfo.setUserPassword(newPassword);
        Result re = new Result();
        re.setCode(congYeRenYuanService.addcongyeRenYuan(tUserInfo));
        return re;
    }

    @GetMapping("/{noid}")
    public Result huixian(@PathVariable String noid){
        TUserInfo tUserInfo = congYeRenYuanService.huixiancongye(noid);
        Result re = new Result();
        if (tUserInfo != null){
            re.ok();
            re.setData(tUserInfo);
        }else {
            re.fail("失败");
        }
        return re;
    }

    @PostMapping("/xiugaicongye")
    public Result xiugaiCongYe(@RequestBody TUserInfo tUserInfo){
        String newPassword = MD5.inputPassToFormPass(tUserInfo.getUserPassword());
        System.out.println("新密码："+newPassword);
        tUserInfo.setUserPassword(newPassword);
        Integer congye = congYeRenYuanService.updacongye(tUserInfo);
        Result re = new Result();
        if (congye > 0){
            re.ok();
        }else {
            re.fail("修改失败");
        }
        return re;
    }

    @PostMapping("/shancongye")
    public Result shanchucongye(@RequestBody List<String> ids){
        Integer shan = congYeRenYuanService.delecongye(ids);
        Result re = new Result();
        if (shan > 0){
            re.ok();
        }else {
            re.fail("删除失败");
        }
        return re;
    }

    @PostMapping("/lizhi")
    public Result congyelizhi(@RequestBody List<String> noid){
        Integer i = congYeRenYuanService.congyelizhi(noid);
        System.err.println(noid);
        Result re = new Result();
        if (i > 0){
            re.ok();
        }else {
            re.fail("离职失败");
        }
        return re;
    }
}
